package com.gitee.swsk33.typehandlerdemo.model;

import lombok.Data;

/**
 * 边界对象类型
 */
@Data
public class Boundary {

	/**
	 * 左上角经纬度
	 * 数组第一个元素为经度，第二个为纬度，后面几个一样
	 */
	private double[] leftTop;

	/**
	 * 右上角经纬度
	 */
	private double[] rightTop;

	/**
	 * 右下角经纬度
	 */
	private double[] rightBottom;

	/**
	 * 左下角经纬度
	 */
	private double[] leftBottom;

	/**
	 * 根据字符串创建Boundary对象
	 * 字符串格式形如："[1,1] [2,2] [3,3] [4,4]"，分别是左上、右上、右下和左下的坐标对
	 *
	 * @param boundaryString 边界字符串
	 * @return 边界对象
	 */
	public static Boundary createFromString(String boundaryString) {
		Boundary result = new Boundary();
		// 分割成坐标对
		String[] coordinates = boundaryString.split(" ");
		// 解析每个坐标对
		for (int i = 0; i < 4; i++) {
			// 获取一个坐标对
			String eachCoordinate = coordinates[i];
			// 去除括号和逗号
			String[] eachCoordinateString = eachCoordinate.substring(1, eachCoordinate.length() - 1).split(",");
			// 解析成数字
			double[] coordinate = new double[2];
			coordinate[0] = Double.parseDouble(eachCoordinateString[0]);
			coordinate[1] = Double.parseDouble(eachCoordinateString[1]);
			// 赋值到属性
			switch (i) {
				case 0:
					result.setLeftTop(coordinate);
					break;
				case 1:
					result.setRightTop(coordinate);
					break;
				case 2:
					result.setRightBottom(coordinate);
					break;
				case 3:
					result.setLeftBottom(coordinate);
					break;
			}
		}
		return result;
	}

	/**
	 * 对象转换成字符串
	 *
	 * @return 字符串格式形如："[1,1] [2,2] [3,3] [4,4]"，分别是左上、右上、右下和左下的坐标对
	 */
	@Override
	public String toString() {
		return String.format("[%f,%f] [%f,%f] [%f,%f] [%f,%f]", leftTop[0], leftTop[1], rightTop[0], rightTop[1], rightBottom[0], rightBottom[1], leftBottom[0], leftBottom[1]);
	}

}